package com.pdg.mcplugin.invisiblehand.commandprocessors;

import org.bukkit.command.CommandSender;
import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;
import com.pdg.mcplugin.invisiblehand.InvisibleHand;
import com.pdg.mcplugin.invisiblehand.dataproviders.Commodity;
import com.pdg.mcplugin.invisiblehand.dataproviders.CommodityKey;

public class AuditCommandProcessor extends CommandProcessorBase<InvisibleHand> {

	private static final String messageFailure = "Could not retrieve information about commodity '%s' from '%s'.";
	private static final String messageCommodityName = "Commodity Name: %s";
	private static final String messageWorldName = "World Name: %s";
	private static final String messageMaterialTypeId = "Material Type Id: %d";
	private static final String messageMaterialData = "Material Data: %d";
	private static final String messageBasePrice = "Base Price: %f";
	private static final String messageBaseSupply = "Base Supply: %f";
	private static final String messageBaseDemand = "Base Demand: %f";
	private static final String messageSupplyFactor = "Supply Factor: %f";
	private static final String messageDemandFactor = "Demand Factor: %f";
	private static final String messageMarkUp = "Mark Up: %f%%";
	private static final String messageSalesCount = "Sales Count: %d";
	private static final String messagePurchaseCount = "Purchase Count: %d";
	private static final String messageStock = "Stock: %d";
	private static final String messageEnabled = "Enabled: %b";
	private static final String messageNotEnoughArguments = "Not enough arguments.";
	private static final int argumentCount = 0;

	public AuditCommandProcessor(InvisibleHand thePlugin) {
		super(thePlugin, new String[]{
				"/ih audit - help with the audit command",
				"/ih audit (commodity-name) - shows information for commodity-name in the current world",
				"/ih audit (commodity-name) (world name) - shows information for commodity-name in world-name"
		});
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		String theCommodityName = theSubCommand;
		String theWorld = Utilities.getWorldIfPlayer(theCommandSender);
		if(!Utilities.checkArgumentCount(theCommandSender, theWorld, theArgumentList, argumentCount, messageNotEnoughArguments)){
			return false;
		}
		if(theWorld==null){
			theWorld = Utilities.getWorldName(theArgumentList);
		}
		CommodityKey theKey = new CommodityKey(theCommodityName,theWorld);
		//TODO
		theCommandSender.sendMessage(theCommodityName);
		theCommandSender.sendMessage(theWorld);
		Commodity theCommodity = getPlugin().getDataProvider().getCommodityTableProvider().retrieve(theKey);
		if(theCommodity==null){
			theCommandSender.sendMessage(String.format(messageFailure,theKey.getName(),theKey.getWorld()));
		}else{
			theCommandSender.sendMessage(String.format(messageCommodityName,theCommodity.getName()));
			theCommandSender.sendMessage(String.format(messageWorldName,theCommodity.getWorld()));
			theCommandSender.sendMessage(String.format(messageMaterialTypeId,theCommodity.getItemStack().getTypeId()));
			theCommandSender.sendMessage(String.format(messageMaterialData,theCommodity.getItemStack().getData().getData()));
			theCommandSender.sendMessage(String.format(messageBasePrice,theCommodity.getBasePrice()));
			theCommandSender.sendMessage(String.format(messageBaseSupply,theCommodity.getBaseSupply()));
			theCommandSender.sendMessage(String.format(messageBaseDemand,theCommodity.getBaseDemand()));
			theCommandSender.sendMessage(String.format(messageSupplyFactor,theCommodity.getSupplyFactor()));
			theCommandSender.sendMessage(String.format(messageDemandFactor,theCommodity.getDemandFactor()));
			theCommandSender.sendMessage(String.format(messageMarkUp,theCommodity.getMarkUp()));
			theCommandSender.sendMessage(String.format(messageSalesCount,theCommodity.getSalesCount()));
			theCommandSender.sendMessage(String.format(messagePurchaseCount,theCommodity.getPurchaseCount()));
			theCommandSender.sendMessage(String.format(messageStock,theCommodity.getStock()));
			theCommandSender.sendMessage(String.format(messageEnabled,theCommodity.getEnabled()));
		}
		return true;
	}

}
